package com.czz.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.portal.entity.Order;
import com.czz.portal.query.OrderQuery;
import com.czz.portal.service.OrderService;
import com.czz.portal.vo.BillVo;
import com.czz.portal.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: OrderController
 * @Author Czz
 * @Date 2020/2/9
 * @Time 16:30
 **/
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
    * @Description 生成订单
    * @Param [order]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/9
    * @Time 18:05
    **/
    @PostMapping(value = "")
    public R saveOrder(Order order){
        if (ObjectUtils.isEmpty(order)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        orderService.saveOrder(order);
        return R.ok().data("item",order.getOrderId());
    }

    /**
    * @Description 生成免费订单
    * @Param [order]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/24
    * @Time 10:35
    **/
    @PostMapping(value = "/free")
    public R saveFreeOrder(Order order){
        if (ObjectUtils.isEmpty(order)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        orderService.saveFreeOrder(order);
        return R.ok();
    }

    /**
    * @Description 根据订单号查询订单
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/9
    * @Time 18:06
    **/
    @GetMapping(value = "{id}")
    public R getOrderById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        OrderVo orderVo = orderService.getOrderVoById(id);
        return R.ok().data("item",orderVo);
    }

    /**
    * @Description 查询用户订单分页列表
    * @Param [page, limit, userId, status]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/10
    * @Time 19:38
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getOrderPageList(@PathVariable Long page, @PathVariable Long limit, OrderQuery orderQuery){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(orderQuery.getUserId())){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<OrderVo> pageParam = new Page<>(page,limit);
        pageParam.setRecords(orderService.getOrderPageList(pageParam,orderQuery));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
    * @Description 根据订单号逻辑删除订单
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/11
    * @Time 11:14
    **/
    @DeleteMapping(value = "{id}")
    public R deleteOrderById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        orderService.removeById(id);
        return R.ok();
    }

    /**
    * @Description 根据订单号取消订单
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/11
    * @Time 11:18
    **/
    @PutMapping(value = "{id}")
    public R cancelOrderById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        orderService.cancelOrderById(id);
        return R.ok();
    }

    /**
    * @Description 查询用户消费记录分页列表
    * @Param [page, limit]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/11
    * @Time 12:04
    **/
    @GetMapping(value = "/bill/{page}/{limit}/{userId}")
    public R getBillPageList(@PathVariable Long page, @PathVariable Long limit, @PathVariable String userId){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(userId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<BillVo> pageParam = new Page<>(page,limit);
        pageParam.setRecords(orderService.getBillVoPageList(pageParam,userId));

        Long total = pageParam.getTotal();
        BigDecimal totalPaid = orderService.getUserTotalPaid(userId);
        return R.ok().data("total", total).data("totalPaid",totalPaid).data("rows", pageParam.getRecords());
    }


    /**
    * @Description 根据课时id/课程id判断是否用户是否购买课程
    * @Param [userId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/16
    * @Time 17:07
    **/
    @GetMapping(value = "/user")
    public R isUserOrder(@RequestParam String userId,
                         @RequestParam(value = "periodId",required = false) String periodId,
                         @RequestParam(value = "courseId",required = false) String courseId){
        if (StringUtils.isEmpty(userId) || (StringUtils.isEmpty(periodId) && StringUtils.isEmpty(courseId))){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Integer count = orderService.isUserOrder(userId,periodId,courseId);
        return R.ok().data("item",count);
    }


    /**
    * @Description 根据视频id判断是否购买课程
    * @Param [userId, videoId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/16
    * @Time 17:59
    **/
    @GetMapping(value = "/user/video")
    public R isUserPlayer(@RequestParam String userId,@RequestParam String videoId){
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(videoId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Integer count = orderService.isUserVideo(userId,videoId);
        return R.ok().data("item",count);
    }

}
